

<template>
  <div>
    <div>Element UI测试</div>
    <el-button type="success" @click="msgBtnClick">{{ msgBtnText }}</el-button>
    <el-button type="success" @click="dllSimple">调用简单的dll函数</el-button>
    <el-button type="success" @click="dllStruct">调用带结构体的dll函数</el-button>
  </div>
  <div class="greetings">
    <h1 class="green">{{ msg }}</h1>
    <h3>
      You’ve successfully created a project with
      <a href="https://vitejs.dev/" target="_blank" rel="noopener">Vite</a> +
      <a href="https://vuejs.org/" target="_blank" rel="noopener">Vue 3</a>.
    </h3>
  </div>
</template>

<script setup>
import {getCurrentInstance, ref} from "vue"
// 获取当前组件实例
const instance = getCurrentInstance();
// 获取当前组件的上下文
// 此方法在开发环境以及生产环境下都能放到组件上下文对象（推荐），相当于vue2中的this
const { proxy }  = getCurrentInstance();  


defineProps({
  msg: {
    type: String,
    required: true
  }
})

let msgBtnText = ref("测试消息提示");

let msgBtnClick = () => {
  msgBtnText.value = "测试消息提示" + new Date().getTime();
 
  let process = window.electron.process;
  let platform = process.platform;
  let os = process.env.OS;
  let osFramework = process.arch === 'x64' || process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432') ? 'x64' : 'x86';
  proxy.$message.success("测试消息提示，当前开发环境位数为：" + platform + "，系统为：" + os + "，系统架构为：" + osFramework);
}

let dllSimple = () =>{
  try {
    let process = window.electron.process;
    const ffi = require("ffi-napi");
    const path = require("path");
    //加载DLL，这种方式在开发环境、生产环境都适用
    const dllPath = path.join(path.resolve(), "resources/library/win32/dll_test.dll");
    console.info("dllPath: " + dllPath);
    proxy.$message.success("dllPath: " + dllPath);
    //引入dll
    const libm = ffi.Library(dllPath, {
      //定义函数，第一个int为返回类型，第二个数组为参数列表
      "test_add": ['int', ['int', 'int']],
    });
    const result = libm.test_add(5, 6);
    console.log("test_add method result：" + result);
    proxy.$message.success("调用DLL成功，返回结果为：" + result);
  } catch (e) {
    proxy.$message.error("调用DLL失败，原因：" + e.toString());
  }
}


let dllStruct = () =>{
    try {
      const path = require("path");
      const ffi = require('ffi-napi');
      const ref = require('ref-napi');
      //末尾的(ref)一定要有
      const StructType = require('ref-struct-di')(ref);
      const RefArray = require('ref-array-di')(ref)

      const Buffer = require('safe-buffer').Buffer
      const iconv = require('iconv-lite');
      //utf-8编码
      let utf8Buf = iconv.encode("这是dll DEMO", 'utf8');

      console.log("utf8Buf: " + utf8Buf.toString("utf8"));

      //申请一个空间
      const out = Buffer.alloc(4096);

      const dllPath = path.join(path.resolve(), "resources/library/win32/dll_test.dll");

      //引入dll
      const libm = ffi.Library(dllPath, {
        //定义函数，第一个int为返回类型，第二个数组为参数列表
        "handler_resp": [ref.types.int, [ref.refType(ref.types.char), ref.types.uint, ref.refType(ref.types.char)]]
      });

     
      let len = libm.handler_resp(utf8Buf, utf8Buf.byteLength, out);
      let outData = out.slice(0, len);
      console.log("out string: " + outData.toString("utf8"));
    } catch (e) {
      proxy.$message.error("调用DLL失败，原因：" + e.toString());
    }
  }

</script>

<style scoped>
h1 {
  font-weight: 500;
  font-size: 2.6rem;
  position: relative;
  top: -10px;
}

h3 {
  font-size: 1.2rem;
}

.greetings h1,
.greetings h3 {
  text-align: center;
}

@media (min-width: 1024px) {
  .greetings h1,
  .greetings h3 {
    text-align: left;
  }
}
</style>
